/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 7/8/2010
 * Time: 9:09 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.Data;
using System.IO;
using System.Collections.Generic;
using Microsoft.Win32;

namespace FileAssociateManager
{
	/// <summary>
	/// Description of Form2.
	/// </summary>
	public partial class Form2 : Form
	{
		public Form2()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			lbx_type.DataSource    = Global.Instance().Data.Tables[0];
			lbx_type.DisplayMember = "Type";
			lbx_type.ValueMember   = "Type";
		}
		
		void Btn_newClick(object sender, EventArgs e)
		{
			string type = ShowInput( "Please enter new file type" );
			if( type == null ) return;
			type =  type.Trim().ToUpper();
			if( lbx_type.FindStringExact( type ) >= 0 ){
				FormLib.alert( "This file type "+type+" is current added in list!" );
				return;
			}
			
			ReturnSet rs = FormLib.openFile( "Exe files(*.exe)|*.exe|All files(*.*)|*.*" );
			if( !rs.Success ) return;
			string file = rs.Result.ToString();
			
			
			
			List<ExtensionCommand> shellCommand = new List<ExtensionCommand>();
			shellCommand.Add( new ExtensionCommand( type, true, "open", "[APP] \"%1\"", file) );
			Extension extension = new Extension( type, "", shellCommand );			
			Global.Instance().AddOrUpdateExtension( extension );
			viewType( type );
		}
		
		string ShowInput(string message){
			string result = null;
			InputDialog dialog = new InputDialog( message );
			dialog.StartPosition = FormStartPosition.CenterScreen;
			dialog.ShowDialog();			
			if( dialog.IsOK ) result = dialog.Content;			
			dialog.Close();
			return result;
		}
		
		
		
		void Lbx_typeMouseClick(object sender, MouseEventArgs e)
		{
			if(lbx_type.SelectedIndex == -1){
				resetViewType();
				return;
			}
			viewType( lbx_type.SelectedValue.ToString() );
			alertEditing(false);
		}
		
				
		void viewType(string type){			
			ReturnSet result = Global.Instance().LoadExtension( type );
			if( !result.Success ){
				FormLib.alert( result.Message );
				return;
			}
			
			Extension extension = (Extension)result.Result;
			
			txt_type.Text = extension.Type;			
			if( extension.Icon == "" ){
				radioButton1.Checked = true;
				txt_icon.Clear();
			}else
			{
				radioButton2.Checked = true;
				txt_icon.Text = extension.Icon;
			}
			
			
			dgv_command.Rows.Clear();
			foreach ( ExtensionCommand command in extension.ShellCommands ){
				dgv_command.Rows.Add( new object[]{				                     	
				                     	command.Default, 
				                     	command.Shell, 
				                     	command.Command, 
				                     	command.Application,
				                     	command.FullPath
				                     });
			}
			
			lbl_newCommand.Enabled = true;
			btn_save.Enabled = true;
			btn_remove.Enabled = true;
			btn_applyone.Enabled = true;
			radioButton1.Enabled = true;
			radioButton2.Enabled = true;
		}
		
		void resetViewType(){
			txt_type.Text = txt_icon.Text = "";	
			dgv_command.Rows.Clear();
			lbl_newCommand.Enabled = false;
			btn_save.Enabled = false;
			btn_remove.Enabled = false;
			radioButton1.Enabled = false;
			radioButton2.Enabled = false;
			btn_applyone.Enabled = false;
		}
		
		void Btn_saveClick(object sender, EventArgs e)
		{
			 string type = txt_type.Text;
			 string icon = radioButton1.Checked ? "" : txt_icon.Text;
			 
			 List<ExtensionCommand> shellCommands = new List<ExtensionCommand>();
			 foreach( DataGridViewRow row in dgv_command.Rows ){
			 	shellCommands.Add(
			 		new ExtensionCommand(
			 			type,	//Type
			 			row.Cells[0].Value.Equals(null) ? false : true, //Default
			 			row.Cells[1].Value.ToString(),	//Shell
			 			row.Cells[2].Value.ToString(),	//Command
			 			row.Cells[4].Value.ToString()	//FullPath
			 		)
			 	);
			 }
			 
			 Extension extension = new Extension( type, icon, shellCommands );
			 
			 Global.Instance().AddOrUpdateExtension( extension );
			 
			 viewType( type );
			 
			 alertEditing(false);
		}
		
		
		void Dgv_commandCellClick(object sender, DataGridViewCellEventArgs e)
		{
			if( e.ColumnIndex == 3 ){
				ReturnSet rs = FormLib.openFile( "Exe files(*.exe)|*.exe|All files(*.*)|*.*" );
				if( !rs.Success ) return;
				string file = rs.Result.ToString();
				dgv_command.Rows[e.RowIndex].Cells[4].Value = file;
				dgv_command.Rows[e.RowIndex].Cells[3].Value = Path.GetFileNameWithoutExtension(file);
				alertEditing(true);
			}
		}
		
		void Lbl_newCommandLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			string cmd = ShowInput( "Please enter new shell command" );
			if( cmd == null ) return;
			cmd =  cmd.Trim().ToLower();
			
			ReturnSet rs = FormLib.openFile( "Exe files(*.exe)|*.exe|All files(*.*)|*.*" );
			if( !rs.Success ) return;
			string file = rs.Result.ToString();
			dgv_command.Rows.Add( new object[]{ false, cmd, "[APP] \"%1\"", Path.GetFileNameWithoutExtension(file), file });
			alertEditing(true);
		}
		
		void RadioButton2CheckedChanged(object sender, EventArgs e)
		{
			txt_icon.Enabled = btn_browse.Enabled = radioButton2.Checked;
			alertEditing(true);
		}
		
		void Btn_applyClick(object sender, EventArgs e)
		{
			Global.Instance().ApplyFileType();			
			FormLib.alert("Apply done");
		}		
		
		void Btn_removeClick(object sender, EventArgs e)
		{
			if(lbx_type.SelectedIndex == -1) return;
			string type = lbx_type.SelectedValue.ToString();
			if( !FormLib.confirm( "Are you sure you want to remove this exntension ["+type+"]" ) ) return;
			Global.Instance().RemoveExtension( type );
			resetViewType();
		}
		
		IconViewer iconviewer;
		void Btn_browseClick(object sender, EventArgs e)
		{
			if( iconviewer == null )
				iconviewer = new IconViewer();
			
			ReturnSet rs = FormLib.openFile( "Accepted files(*.ico;*.dll;*.exe)|*.ico;*.dll;*.exe|All files(*.*)|*.*" );
			if( !rs.Success ) return;
			iconviewer.ViewIconFromFile( rs.Result.ToString() );
			if( !iconviewer.IsOK ) return;
			txt_icon.Text = iconviewer.Filename;
		}
		
		void Btn_applyoneClick(object sender, EventArgs e)
		{
			if(lbx_type.SelectedIndex == -1) return;
			string type = lbx_type.SelectedValue.ToString();
			Global.Instance().ApplyFileType( type );
			FormLib.alert("Apply done");
		}
	
		void alertEditing(bool alert){
			txt_type.BackColor = alert ? Color.Red : SystemColors.Control;
		}
	}
}
